<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\User;
class UserAddress extends Model
{
    protected $fillable = [
        'province',
        'city',
        'district',
        'address',
        'zip',
        'contact_name',
        'contact_phone',
        'is_default',
        ];

    protected $dates = ['last_used_at'];

    protected $appends = ['full_address'];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function getFullAddressAttribute()
    {
        return "{$this->province}{$this->city}{$this->district}{$this->address}";
    }

    public static function getDefault($user_id)
    {
        $where = [
            ['user_id', '=', $user_id],
            ['is_default', '=', 1],
        ];
        $addr = self::where($where)->first();
        if ($addr) {
            $addr = $addr->toArray();
        }
        return $addr;
    }

}
